from fastapi import APIRouter, Depends, Form, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_admin.system.service.table_service import TableService
from module_admin.system.entity.vo.table_vo import DeleteTableModel, TableModel, TablePageQueryModel
from utils.common_util import bytes2file_response
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil


tableController = APIRouter(prefix='/system/table', dependencies=[Depends(LoginService.get_current_user)])


@tableController.get(
    '/list', response_model=PageResponseModel, dependencies=[Depends(CheckUserInterfaceAuth('system:table:list'))]
)
async def get_system_table_list(
    request: Request,
table_page_query: TablePageQueryModel = Depends(TablePageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    table_page_query_result = await TableService.get_table_list_services(query_db, table_page_query, is_page=True)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=table_page_query_result)


@tableController.post('', dependencies=[Depends(CheckUserInterfaceAuth('system:table:add'))])
@ValidateFields(validate_model='add_table')
@Log(title='这是一个示例', business_type=BusinessType.INSERT)
async def add_system_table(
    request: Request,
    add_table: TableModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_table_result = await TableService.add_table_services(query_db, add_table)
    logger.info(add_table_result.message)

    return ResponseUtil.success(msg=add_table_result.message)


@tableController.put('', dependencies=[Depends(CheckUserInterfaceAuth('system:table:edit'))])
@ValidateFields(validate_model='edit_table')
@Log(title='这是一个示例', business_type=BusinessType.UPDATE)
async def edit_system_table(
    request: Request,
    edit_table: TableModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    edit_table.update_by = current_user.user.user_name
    edit_table.update_time = datetime.now()
    edit_table_result = await TableService.edit_table_services(query_db, edit_table)
    logger.info(edit_table_result.message)

    return ResponseUtil.success(msg=edit_table_result.message)


@tableController.delete('/{ids}', dependencies=[Depends(CheckUserInterfaceAuth('system:table:remove'))])
@Log(title='这是一个示例', business_type=BusinessType.DELETE)
async def delete_system_table(request: Request, ids: str, query_db: AsyncSession = Depends(get_db)):
    delete_table = DeleteTableModel(ids=ids)
    delete_table_result = await TableService.delete_table_services(query_db, delete_table)
    logger.info(delete_table_result.message)

    return ResponseUtil.success(msg=delete_table_result.message)


@tableController.get(
    '/{id}', response_model=TableModel, dependencies=[Depends(CheckUserInterfaceAuth('system:table:query'))]
)
async def query_detail_system_table(request: Request, id: int, query_db: AsyncSession = Depends(get_db)):
    table_detail_result = await TableService.table_detail_services(query_db, id)
    logger.info(f'获取id为{id}的信息成功')

    return ResponseUtil.success(data=table_detail_result)


@tableController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('system:table:export'))])
@Log(title='这是一个示例', business_type=BusinessType.EXPORT)
async def export_system_table_list(
    request: Request,
    table_page_query: TablePageQueryModel = Form(),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取全量数据
    table_query_result = await TableService.get_table_list_services(query_db, table_page_query, is_page=False)
    table_export_result = await TableService.export_table_list_services(table_query_result)
    logger.info('导出成功')

    return ResponseUtil.streaming(data=bytes2file_response(table_export_result))
